package com.it.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.dao.BolgMapper;
import com.it.domain.BlogEntity;
import com.it.exception.NotFoundException;
import com.it.service.BlogService;
import com.it.service.BlogTypeService;
import com.it.service.UserService;
import com.it.util.MarkdownUtils;
import com.it.util.PageUtils;
import com.it.util.Query;
import com.it.vo.BlogQueryVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

;

/**
 * @author : code1997
 * @date : 2021/9/9 22:46
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BolgMapper, BlogEntity> implements BlogService {


    @Autowired
    BlogTypeService blogTypeService;

    @Autowired
    UserService userService;

    @Override
    public BlogEntity getBolg(Long id) {
        BlogEntity blogEntity = this.baseMapper.selectById(id);
        blogEntity.setType(blogTypeService.getType(blogEntity.getTypeId()));
        blogEntity.setUser(userService.getById(blogEntity.getUserId()));
        return blogEntity;
    }

    @Transactional
    @Override
    public BlogEntity getBolgAndConvert(Long id) {
        BlogEntity tempBlog = this.baseMapper.selectById(id);
        if (tempBlog == null) {
            throw new NotFoundException("该博客不存在！");
        }
        //防止类似于hibernate的缓存的影响
        BlogEntity temp = new BlogEntity();
        BeanUtils.copyProperties(tempBlog, temp);
        String content = temp.getContent();
        /*将markdown文本转换为html*/
        temp.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        temp.setUser(userService.getById(temp.getUserId()));
        temp.setType(blogTypeService.getType(temp.getTypeId()));
        this.baseMapper.updateViews(id);
        return temp;
    }

    @Override
    public PageUtils listBlog(Map<String, Object> params, BlogQueryVo blog) {
        QueryWrapper<BlogEntity> queryWrapper = new QueryWrapper<>();
        System.out.println("current blog:" + blog);
        if (!StringUtils.isBlank(blog.getTitle())) {
            queryWrapper.like("title", blog.getTitle());
        }
        if (blog.getTypeId() != null) {
            queryWrapper.eq("type_id", blog.getTypeId());
        }
        if (blog.isRecommend()) {
            queryWrapper.eq("recommend", blog.isRecommend());
        }
        PageUtils page = new PageUtils(this.page(new Query<BlogEntity>().getPage(params, "update_time", false), queryWrapper));
        page.getList().forEach(temp -> {
            BlogEntity tempBlog = (BlogEntity) temp;
            tempBlog.setType(blogTypeService.getType(tempBlog.getTypeId()));
            tempBlog.setUser(userService.getById(tempBlog.getUserId()));
        });
        return page;
    }

    @Override
    public PageUtils listBlog(Map<String, Object> params) {
        PageUtils pageUtils = new PageUtils(this.page(new Query<BlogEntity>().getPage(params), new QueryWrapper<>()));
        pageUtils.getList().forEach(temp -> {
            BlogEntity tempBlog = (BlogEntity) temp;
            tempBlog.setUser(userService.getById(tempBlog.getUserId()));
            tempBlog.setType(blogTypeService.getType(tempBlog.getTypeId()));
        });
        return pageUtils;
    }

    /**
     * todo:根据tagsId来分页查询
     */
    @Override
    public PageUtils listBlogByTagId(Map<String, Object> params) {
        return listBlog(params);
    }

    @Override
    public PageUtils listBlogByQuery(Map<String, Object> params) {
        return new PageUtils(this.page(new Query<BlogEntity>().getPage(params), new QueryWrapper<>()));
    }

    @Override
    public List<BlogEntity> listRecommedBlogTop(Integer size) {
        return this.baseMapper.listRecommedBlogTop(size);
    }

    @Transactional
    @Override
    public BlogEntity saveBlog(BlogEntity blog) {
        if (blog.getId() == null) {
            blog.setCreateTime(new Date());
            blog.setUpdateTime(new Date());
            blog.setViews(0);
        } else {
            blog.setUpdateTime(new Date());
        }
        this.baseMapper.insert(blog);
        return blog;
    }

    @Transactional
    @Override
    public BlogEntity updateBlog(Long id, BlogEntity blog) {
        BlogEntity tempBlog = this.baseMapper.selectById(id);
        if (tempBlog == null) {
            throw new NotFoundException("不存在该博客");
        }
        BeanUtils.copyProperties(blog, tempBlog);
        tempBlog.setUpdateTime(new Date());
        this.baseMapper.updateById(tempBlog);
        return tempBlog;
    }

    @Override
    public void deleteBlog(Long id) {
        this.baseMapper.deleteById(id);
    }

    @Override
    public Map<String, List<BlogEntity>> archivesBlog() {
        //1.获取全部的年份
        List<String> groupYear = this.baseMapper.findGroupYear();
        //2.根据年份分组获取所有的博客信息
        Map<String, List<BlogEntity>> blogs = new TreeMap<>((o1, o2) -> -o1.compareTo(o2));
        for (String s : groupYear) {
            blogs.put(s, this.baseMapper.findByYear(s));
        }
        return blogs;
    }

    @Override
    public Long getBlogCount() {
        return this.baseMapper.selectCount(new QueryWrapper<>()).longValue();
    }
}
